<?php
namespace ac\modules\developer\controllers;

use Yii;
use ac\controllers\AuthController;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\Error;
use common\models\OperationLog;


class LogController extends AuthController
{

	/*错误列表*/
    public function actionErrorLogList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $error = new Error;
        $count = $error::find()->count();
        $errorList = $error::find()
                    ->select(['e_id', 'status_code', 'obj_class', 'message', 'file', 'line', 'url', 'state', 'add_time'])
                    ->offset($pageSize*($currPage-1))->limit($pageSize)
                    ->orderBy(['add_time' => SORT_DESC, 'e_id' => SORT_DESC])->asArray()->all();
        $res = array(
            'list' => $errorList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*错误详情*/
    public function actionErrorDetial()
    {
        echo 'detail';
    }

    /*设置状态*/
    public function actionSetErrorState()
    {
        $id = isset($this->post['id'])?$this->post['id']:'';
        $flag = isset($this->post['flag'])?$this->post['flag']:'';
        $type = gettype($id);
        if($id and ($type == 'string' or $type == 'array') and in_array($flag, [1,2,3,4])) {
            $error = new Error;
            if($type == 'string'){//单个
                if($error->updateAll(['state' => $flag], 'e_id = :id', [':id' => $id])){//成功
                    $backInfo = [0, '更改错误日志状态成功'];//返回信息
                }else{//失败
                    $backInfo = ErrMap::getError(10013, '更改错误日志状态失败');//返回信息
                }
            }
            if($type == 'array'){//批量
                if($error->updateAll(['state' => $flag], ['in', 'e_id', $id])){//成功
                    $backInfo = [0, '更改错误日志状态成功'];//返回信息
                }else{//失败
                    $backInfo = ErrMap::getError(10013, '更改错误日志状态失败');//返回信息
                }
            }

            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), '') ) != 'boolean'){
                return $res;
            };
        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除错误（真删除）*/
    public function actionDeleteErrorLog()
    {
        $id = isset($this->post['id'])?$this->post['id']:'';
        // P(gettype($id));
        $type = gettype($id);
        if($id and ($type == 'string' or $type == 'array')){
            $error = new Error;
            if($type == 'string'){//单个删除
                if($error->deleteAll('e_id = :id', [':id' => $id])){//删除成功
                    $backInfo = [0, '删除错误日志成功'];//返回信息
                }else{//删除失败
                    $backInfo = ErrMap::getError(10013, '删除错误日志失败');//返回信息
                }
            }
            if($type == 'array'){//批量删除
                if($error->deleteAll(['in', 'e_id', $id])){//删除成功
                    $backInfo = [0, '删除错误日志成功'];//返回信息
                }else{//删除失败
                    $backInfo = ErrMap::getError(10013, '删除错误日志失败');//返回信息
                }
            }

            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), '') ) != 'boolean'){
                return $res;
            };
        }

        return Tool::showRes(ErrMap::getError(10005));
    }


    /*===========================下面是操作日志===============================*/

    /*操作列表*/
    public function actionOperationLogList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $operationLog = new OperationLog;
        $count = $operationLog::find()->count();
        $operationList = $operationLog::find()
                        ->select(['log_id', 'loginname', 'back_code', 'message', 'back_info', 'model_error', 'state', 'add_time'])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->orderBy(['add_time' => SORT_DESC, 'log_id' => SORT_DESC])->asArray()->all();
        $res = array(
            'list' => $operationList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*删除错误（真删除）*/
    public function actionDeleteOperationLog()
    {
        $id = isset($this->post['id'])?$this->post['id']:'';
        // P(gettype($id));
        $type = gettype($id);
        if($id and ($type == 'string' or $type == 'array')){
            $operationLog = new OperationLog;
            if($type == 'string'){//单个删除
                if($operationLog->deleteAll('log_id = :id', [':id' => $id])){//删除成功
                    $backInfo = [0, '删除操作日志成功'];//返回信息
                }else{//删除失败
                    $backInfo = ErrMap::getError(10013, '删除操作日志失败');//返回信息
                }
            }
            if($type == 'array'){//批量删除
                if($operationLog->deleteAll(['in', 'log_id', $id])){//删除成功
                    $backInfo = [0, '删除操作日志成功'];//返回信息
                }else{//删除失败
                    $backInfo = ErrMap::getError(10013, '删除操作日志失败');//返回信息
                }
            }

            //操作日志
            // if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), '') ) != 'boolean'){
            //     return $res;
            // };
            return Tool::showRes($backInfo);
        }

        return Tool::showRes(ErrMap::getError(10005));
    }

}